<script setup>
import { ref, onMounted } from "vue";
import { useCreate } from "@/store/useCreate.js";
import { ElMessage } from "element-plus";
import { storeToRefs } from "pinia";

const createStore = useCreate();
const { formData } = storeToRefs(createStore);
const labels = ref([]);
const labelOptions = ref([]);

onMounted(() => {
  labels.value = formData.value.labelList.map((item) => item.label);
});

const confirm = () => {
  if (labels.value.length === 0) {
    return ElMessage.warning("请输入商品标签");
  }
  // 更新createStore.formData.labelList
  const newList = [];
  // const oldLabels = createStore.formData.labelList.filter((item) =>
  //   labels.value.includes(item.label)
  // );
  // newList.push(...oldLabels);
  for (let i = 0; i < labels.value.length; i++) {
    newList.push({
      label: labels.value[i],
    });
  }
  createStore.formData.labelList = newList;
  createStore.setCurrentStepId(11);
  createStore.setRequiredNumber(9, 0);
};

// 选择标签
const selectChange = (value) => {
  // 限制最多6个,如果超过6个,则删除最后一个
  if (value.length > 6) {
    ElMessage.warning("最多允许6个标签");
    value.pop();
  }

  // 检查每个标签的字数限制
  const newValue = value.map((tag) => {
    if (tag.length > 10) {
      ElMessage.warning(`标签"${tag}"超过10个字，将被截断`);
      return tag.substring(0, 10);
    }
    return tag;
  });

  // 更新标签值
  labels.value = newValue;
};
</script>

<template>
  <div class="header">
    <div class="left">
      <h4>商品标签</h4>
      <p>添加技能标签，有助于平台分发你的服务（最多6个）</p>
    </div>
  </div>
  <div class="select-label">
    <!--    <el-select-->
    <!--        v-model="labels"-->
    <!--        multiple-->
    <!--        placeholder="请选择..."-->
    <!--        style="width: 240px"-->
    <!--    >-->
    <!--      <el-option-->
    <!--          v-for="item in labelOptions"-->
    <!--          :key="item.value"-->
    <!--          :label="item.label"-->
    <!--          :value="item.value"-->
    <!--      />-->
    <!--    </el-select>-->
    <el-select
      style="height: 68px; margin-top: 16px; border-radius: 8px; width: 650px"
      v-model="labels"
      multiple
      filterable
      allow-create
      default-first-option
      :reserve-keyword="false"
      placeholder="请输入商品标签"
      @change="selectChange"
    >
      <el-option
        v-for="item in createStore.formData.labelList"
        :key="item.label"
        :label="item.label"
        :value="item.label"
      />
    </el-select>
    <p class="input-label">提示：多个标签用空格隔开，最多6个，每个标签最多10个字</p>
  </div>
  <p class="btn"><el-button class="button" @click="confirm">确认</el-button></p>
</template>

<style scoped lang="scss">
.header {
  display: flex;
  align-items: center;

  .left {
    flex: 1;

    h4 {
      font-size: 16px;
      margin-bottom: 8px;
    }

    p {
      color: rgba(25, 30, 40, 0.48);
      font-size: 14px;
      span {
        color: #1dbf73;
      }
    }
  }

  .confirm-btn {
    width: 160px;
    height: 48px;
    background: #1dbf73;
    border-radius: 8px 8px 8px 8px;
    color: #fff;
    font-size: 16px;
  }
}
.btn {
  margin-top: 36px;
  .button {
    width: 160px;
    height: 48px;
    background: #1dbf73;
    border-radius: 8px 8px 8px 8px;
    color: #fff;
  }
}
.input-label {
  color: #999;
}
</style>

<style>
.select-label .el-select {
  height: auto !important;
}
.select-label .el-select__wrapper {
  min-height: 48px;
  height: inherit !important;
  border-radius: 8px 8px 8px 8px;
  border: 1px solid rgba(25, 30, 40, 0.08);
  box-shadow: none;
  margin-top: 16px;
  width: 650px;
}
.select-label .el-select__selected-item {
  height: 32px;
}
.select-label .el-tag.is-closable {
  height: 32px;
}
</style>
